package org.adream.account.service;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.adream.account.dao.EconomicTypeDao;
import org.adream.account.entity.EconomicTypeEntity;
import org.adream.account.model.ResultModel;
import org.adream.account.util.Constant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class EconomicTypeService {
	
	private final static Logger logger = LoggerFactory.getLogger(EconomicTypeService.class);
	
	@Autowired
	private EconomicTypeDao economicTypeDao;
	
	@Autowired
	private HttpSession session;
	
	/**
	 * 新增
	 * @param economicType
	 */
	public ResultModel<String> addEconomicType(EconomicTypeEntity economicType) {
		String operator = (String) session.getAttribute("uid");
		economicType.setCreator(operator);
		economicType.setModifier(operator);
		economicType.setDr(Constant.ONE);
		int result = economicTypeDao.addEconomicType(economicType);
		if(result == 0) {
			logger.warn("新增经济类型数据失败,数据库错误,etName:{}", economicType.getEtname());
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 删除 逻辑删除
	 * @param etid
	 */
	public ResultModel<String> deleteEconomicType(Integer etid) {
		EconomicTypeEntity economicType = economicTypeDao.queryEconByEtidAndDr(etid, Constant.ONE);
		if(economicType == null) {
			logger.warn("删除经济类型失败,数据不存在,etid:{}", etid);
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "删除失败,数据不存在", null);
		}
		String modifier = (String)session.getAttribute("uid");
		economicType.setModifier(modifier);
		economicType.setDr(Constant.ZERO);
		int result = economicTypeDao.deleteEconomicType(economicType);
		if(result == 0) {
			logger.warn("删除经济类型失败,数据库错误,etid:{}", etid);
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 更新
	 * @param etid
	 * @param economicTypeEntity
	 */
	public ResultModel<String> updateEconomicType(Integer etid,EconomicTypeEntity economicTypeEntity) {
		EconomicTypeEntity economicType = economicTypeDao.queryEconByEtidAndDr(etid, Constant.ONE);
		if(economicType == null) {
			logger.warn("更新经济类型失败,数据不存在,etid:{}", etid);
			return new ResultModel<String>(ResultModel.NOT_FOUND_OBJECT, "删除失败,数据不存在", null);
		}
		String modifier = (String) session.getAttribute("uid");
		economicType.setEtname(economicTypeEntity.getEtname());
		economicType.setModifier(modifier);
		int result = economicTypeDao.updateEconomicType(economicType);
		if(result == 0) {
			logger.warn("更新经济类型数据失败,数据库错误,etid:{}", etid);
			return new ResultModel<String>(ResultModel.ERR_SYS, "系统错误,请联系系统管理员", null);
		}
		return new ResultModel<String>(ResultModel.SUCCESS, ResultModel.SUCCESS_MSG, null);
	}
	
	/**
	 * 根据删除标志查询
	 * @param dr 删除标志
	 * @return
	 */
	public List<EconomicTypeEntity> queryEconomicTypeByDr(Integer dr) {
		return economicTypeDao.queryEconomicTypeByDr(dr);
	}
	
	/**
	 * 根据etid,删除标志查询
	 * @param etid
	 * @param dr
	 * @return
	 */
	public EconomicTypeEntity queryEconByEtidAndDr(Integer etid,Integer dr) {
		return economicTypeDao.queryEconByEtidAndDr(etid, dr);
	}
}